package com.lb.trac.util;


import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import com.lb.trac.service.SearchService;

public class HibernateBrowser extends AbstractBrowser {

	
	/*
	 * (non-Javadoc)
	 * 
	 * @seecom.setup.trac.util.Browser#impaginaRicerca(javax.servlet.http.
	 * HttpServletRequest, com.lb.trac.util.SQLCondition, java.lang.Class)
	 */
	
	private int itemsPerPage;
	
	public Page impaginaRicerca(HttpServletRequest request, Object object,
			Class<?> clazz) {
		List<Page> pages = (List<Page>) request.getSession(false).getAttribute(
				"book");
		String page = request.getParameter(getParamPageName());
		Page thePage = null;
		int p = page == null ? 1 : Integer.parseInt(page);
		if (page == null) {
			request.getSession().removeAttribute("lastFirstRecord");
			pages = new ArrayList<Page>();
		}
		// Profilo profilo = loginForm.getUtenti().getProfilo();
		// if (show == null || "false".equalsIgnoreCase(show)) {
		// if (profilo.getUtenteAmministratore()) {
		// tickets = searchService.findTickets(0);
		// } else {

//		if (page == null) {
			
			pages = preparaImpaginazione(request, count(getSearchService(),clazz,object));
			if (pages.size() > 0) {
				request.getSession(false).setAttribute("book", pages);

			}
//		}

		thePage = pages.get(p - 1);

		// }
		request.setAttribute("nextPage", new Integer(p + 1));
		request.setAttribute("prevPage", new Integer(p - 1));
		request.setAttribute("currentPage", thePage);
		return thePage;
	}

	public Integer count(Object... objects){
		return (Integer) ((SearchService)objects[0]).count((Class<?>) objects[1], (SQLCondition) objects[2]);
	}

	public int getItemsPerPage() {
		return itemsPerPage;
	}

	public void setItemsPerPage(int itemsPerPage) {
		this.itemsPerPage = itemsPerPage;
	}
}
